<template>
  <el-container class="container">
    <el-header class="header">
      <div class="name">后台管理系统</div>
      <el-dropdown @command="handleCommand">
        <span class="username">
          {{ user.username }}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="logout">退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <el-container style="height: calc(100% - 60px)">
      <el-aside width="200px" class="aside">
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          router
          background-color="#ADD8E6"
          text-color="#257671"
          active-text-color="#fff"
        >
          <el-menu-item index="/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">系统管理</span>
          </el-menu-item>
          <el-menu-item index="/users">
            <i class="el-icon-user-solid"></i>
            <span slot="title">用户管理</span>
          </el-menu-item>
          <el-menu-item index="/carousel/list">
            <i class="el-icon-picture"></i>
            <span slot="title">轮播图列表</span>
          </el-menu-item>
          <el-menu-item index="/carousel/add">
            <i class="el-icon-picture-outline"></i>
            <span slot="title">添加轮播图</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  name: "Layout",
  data() {
    return {
      user: this.getUserFromLocalStorage(),
    };
  },
  methods: {
    getUserFromLocalStorage() {
      try {
        const user = localStorage.getItem("user");
        return user ? JSON.parse(user) : {};
      } catch (error) {
        console.error("Failed to parse user from localStorage", error);
        return {};
      }
    },
    handleCommand(command) {
      if (command === "logout") {
        this.logout();
      }
    },
    logout() {
      localStorage.removeItem("authorization");
      localStorage.removeItem("user");
      this.$router.replace("/login");
    },
  },
};
</script>

<style scoped>

.header .name {
  color: #257671;
}
.header .username {
  cursor: pointer;
  color: #ffffff;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #b1dede;
}

.container {
  height: 100%;
}

.aside {
  background-color: #b1dede;
  height: 100%;
}



</style>
